home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5181 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  82 lines

  1. Path: s02.pavilion.co.uk!usenet
  2. From: AJRobb@pavilion.co.uk (Andy J Robb)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: RETURN ();
  5. Date: Thu, 08 Feb 1996 06:59:40 GMT
  6. Organization: Pavilion Internet plc
  7. Message-ID: <4fc6ve$kbn@s02.pavilion.co.uk>
  8. References: <DMFxxq.7M7@emi.net>
  9. NNTP-Posting-Host: poolb09.pavilion.co.uk
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. samstar@emi.net wrote:
  13.  
  14. >Is there a way to return muliple values to main from a seperate function ?
  15.  
  16. >ex :
  17.  
  18. >int main(void)
  19.  
  20. main(int, char**)    /* This says "ignore the command line" */
  21.  
  22. >{
  23. >  Input_data();
  24. >  printf("Here all the input data");
  25. >  
  26. >  return 0;
  27. >}
  28.  
  29. >int Input_data()
  30. >{
  31. >  x=5,y=30,z=10    /* These were gotten by asking questions */
  32. It is not a good idea to use commas like this.
  33. >            /* using printf / scanf/fgets (what ever)  */
  34. >            /*point is they are gotten by question      */
  35.  
  36. >return(x,y,z);
  37.  
  38. This evaluates x then y and finally z returning the value (int)z
  39.  
  40. >}
  41.  
  42. >how will main be able to see these values ?
  43.  
  44. main will only see the last value.
  45.  
  46. There are several ways to do this.  Arguably the clearest and most
  47. robust is to pass the addresses of the required results.  There are
  48. other ways (including global variables) but these make the program
  49. more difficult to maintain.
  50.  
  51. #include <stdio.h>
  52.  
  53. void Input_data(int *x, int *y, int *z)
  54.   *x = 5;
  55.   *y = 30;
  56.   *z = 10;
  57. }
  58.  
  59. main(int, char**)
  60. { int x, y, z;
  61.   Input_data(&x, &y, &z);
  62.  
  63.   printf("here are the data: x=\"%d\", y=\"%d\", z=\"%d\"\n", x,y,z);
  64.  
  65.   return 0;
  66. }
  67.  
  68. Regards,
  69. Andy Robb.
  70. -----BEGIN PGP PUBLIC KEY BLOCK-----
  71. Version: 2.6.2i
  72.  
  73. mQCNAy/MpRwAAAEEAOt6uBYqT8yv9EmqNhK8m6v+bYi8QjnGW3Bo6iU1gsMj5pa6
  74. MHgq99c8deADbE3cbJ6uZS9v5pZE3WCf6HCQjlB5iULA5RZzMdAumd/WUzuL9UT3
  75. B44D9EqqFIL79FlYb56v4oKFqFp1/J2bIpYUwnUvabGzGjdLrpPl4P16x9sNAAUR
  76. tCNBbmR5IEogUm9iYiA8QUpSb2JiQHBhdmlsaW9uLmNvLnVrPrQhQW5keSBSb2Ji
  77. IDxBSlJvYmJAcGF2aWxpb24uY28udWs+
  78. =/wVD
  79. -----END PGP PUBLIC KEY BLOCK-----
  80.  
  81.